/*
* @Author: yyf
* @Create: 2018/4/25
* @description: 事件——投资方机构、投资人、企业
*/
<template>
    <div>
        <el-form label-width="120px">
            <el-row v-if="formTitle==='org'">
                <el-col :span="14">
                    <el-form-item label="投资机构">
                        <el-input v-model="companySearch.name"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label-width="10px">
                        <el-select v-model="companySearch.searchType" class="w-100">
                            <el-option 
                                v-for="item in openSearchNameType" 
                                :key="item.dictValue" 
                                :value="item.dictValue" 
                                :label="item.dictName">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="2">
                    <el-form-item label-width="10px">
                        <el-button type="primary" @click="getEnterprise">搜索</el-button>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row v-if="formTitle==='investor'">
                <el-col :span="20">
                    <el-form-item label="投资人">
                        <el-input v-model="companySearch.name"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="2">
                    <el-form-item label-width="10px">
                        <el-button type="primary" @click="getPersonInfo">搜索</el-button>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row v-if="formTitle==='enterprise'">
                <el-col :span="14">
                    <el-form-item label="企业名称">
                        <el-input v-model="companySearch.name"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label-width="10px">
                        <el-select v-model="companySearch.searchType" class="w-100">
                            <el-option 
                                v-for="item in openSearchNameType" 
                                :key="item.dictValue" 
                                :value="item.dictValue" 
                                :label="item.dictName">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="2">
                    <el-form-item label-width="10px">
                        <el-button type="primary" @click="getEnterprise">搜索</el-button>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row v-if="formTitle==='org'">
                <el-form-item label="基金">
                    <el-input v-model="companyFundName" disabled></el-input>
                </el-form-item>
            </el-row>
            <el-row v-if="formTitle==='org'">
                <el-col :span="12">
                    <el-form-item label="机构经理人">
                        <el-select v-model="eventInvestDto.orgPersonIds" class="w-100" multiple>
                            <el-option 
                                v-for="item in orgPersonIds" 
                                :key="item.personId" 
                                :value="item.personId" 
                                :label="item.personName">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="基金经理人">
                        <el-select v-model="eventInvestDto.fundPersonIds" class="w-100" multiple>
                            <el-option 
                                v-for="item in fundPersonIds" 
                                :key="item.personId" 
                                :value="item.personId" 
                                :label="item.personName">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="实际投资金额">
                        <el-input v-model="eventInvestDto.investMoney">
                            <template slot="append">万</template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="币种">
                        <el-select v-model="eventInvestDto.currencyType" class="w-100">
                            <el-option 
                                v-for="item in currencyType" 
                                :key="item.dictValue" 
                                :value="item.dictValue-0" 
                                :label="item.dictName">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="估计投资金额">
                        <el-input v-model="eventInvestDto.investEstimate">
                            <template slot="append">万</template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="交易量级">
                        <el-select v-model="eventInvestDto.investDisplay" class="w-100">
                            <el-option 
                                v-for="item in tradeMagnitude" 
                                :key="item.dictValue" 
                                :value="item.dictValue-0" 
                                :label="item.dictName">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="交易股权">
                        <el-input v-model="eventInvestDto.stock">
                            <template slot="append">%</template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="交易地区类型">
                        <el-select v-model="eventInvestDto.areaType" class="w-100">
                            <el-option 
                                v-for="item in tradeAreaType" 
                                :key="item.dictValue" 
                                :value="item.dictValue-0" 
                                :label="item.dictName">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row v-if="eventType === 17">
                <el-col :span="12">
                    <el-form-item label="交易后股权">
                        <el-input v-model="eventInvestDto.stockAfter">
                            <template slot="append">%</template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="取得控制权">
                        <el-select v-model="eventInvestDto.controlStatus" class="w-100">
                                <el-option 
                                    v-for="item in whether" 
                                    :key="item.dictValue" 
                                    :value="item.dictValue-0" 
                                    :label="item.dictName">
                                </el-option>
                            </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row v-if="eventType === 17">
                <el-col :span="12">
                    <el-form-item label="投资地位">
                        <el-select v-model="eventInvestDto.investStatus" class="w-100">
                            <el-option 
                                v-for="item in investmentStatus" 
                                :key="item.dictValue" 
                                :value="item.dictValue-0" 
                                :label="item.dictName">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <el-row class="align-center">
            <el-button type="primary" @click="saveInvestor" round>保存</el-button>
            <el-button @click="closeDialog(false)" round>取消</el-button>
        </el-row>
        <el-dialog :visible.sync="enterpriseDialog" append-to-body>
            <CompanyInfoTable :mainCompanySearch="mainCompanySearch"
                            ref="companyTable"
                            @getName="getName">
            </CompanyInfoTable>
        </el-dialog>
        <el-dialog :visible.sync="personInfoDialog" append-to-body>
            <el-table :data="personInfoData">
                <el-table-column label="ID" prop="personId"></el-table-column>
                <el-table-column label="中文名" prop="personName">
                    <template slot-scope="scope">
                        <el-button type="text" @click="getPersonName(scope.row.personId, scope.row.personName)">{{scope.row.personName}}</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="企业名称" prop="fullName"></el-table-column>
                <el-table-column label="身份属性" prop="businessType"></el-table-column>
                <el-table-column label="职位" prop="title"></el-table-column>
                <el-table-column label="入职时间" prop="entryTime"></el-table-column>
            </el-table>
        </el-dialog>
    </div>
</template>

<script lang="ts">
import Vue from 'vue';
import { Component, Prop } from 'vue-property-decorator';
import { EventInvestDto, CompanyOpenSearchVO, InvestorPersonVo, ComManagerVo } from '../../../api/Interface';
import { CompanySearch, ManageSearch } from '../../../service/common/interface';
import { PersonSearchDto } from '../../../service/event/Interface';
import EventInvestorService from '../../../service/event/investor/index';
import CommonService from '../../../service/common/index';
import EventService from '../../../service/event/index';
import ComFundService from '../../../service/subject/company/fund-info/index';
import CompanyInfoTable from '../../companyTable/index.vue';
@Component({
    components: {
        CompanyInfoTable, // 企业信息搜索table
    }
})
export default class InvestFormDialog extends Vue {
    @Prop({ default: 'org' })
    public formTitle: string;
    @Prop({ default: 16 })
    public eventType: number;
    @Prop()
    public eventInvestDto: EventInvestDto;

    // 字典中获取下拉菜单数据
    public openSearchNameType = this.$store.state.user.dictionary.dicList.openSearchNameType; // 投资机构
    public currencyType = this.$store.state.user.dictionary.dicList.currencyType; // 币种
    public exitType = this.$store.state.user.dictionary.dicList.exitType; // 退出方式
    public exchangeType = this.$store.state.user.dictionary.dicList.exchangeType; // 退出交易所
    public tradeMagnitude = this.$store.state.user.dictionary.dicList.tradeMagnitude; // 交易量级
    public whether = this.$store.state.user.dictionary.dicList.whether; // 是/否
    public tradeAreaType = this.$store.state.user.dictionary.dicList.tradeAreaType; // 交易地区类型
    public investmentStatus = this.$store.state.user.dictionary.dicList.investmentStatus; // 投资地位
    // 初始化数据
    public companyOpenSearch: CompanyOpenSearchVO = {} as CompanyOpenSearchVO; // 企业信息
    public companySearch: CompanySearch = { name: '', searchType: 'full_name' } as CompanySearch; // 根据条件搜索企业
    public personSearchDto: PersonSearchDto = {} as PersonSearchDto; // 根据条件搜索人物信息
    public manageSearch: ManageSearch = {} as ManageSearch; // 根据条件搜索机构人物基金人物
    public eventId: string = ''; // 事件ID
    public businessId: string = ''; // 投资方id （机构id、投资人id、企业id）
    public tabIndex: string = '0';
    public attribute: number = 2; // 搜索企业的参数
    public enterpriseDialog: boolean = false; // 企业信息弹框
    public personInfoDialog: boolean = false; // 人物信息弹框
    public mainCompanySearch = {};
    public companyFundName: string = ''; // 基金名称
    public fundId: number | null = null; // 基金ID
    public personInfoData: Array<InvestorPersonVo> = []; // 人物信息数据
    public fundPersonIds: Array<ComManagerVo> = []; // 基金人物信息数据
    public orgPersonIds: Array<ComManagerVo> = []; // 机构人物信息数据
    public fundPersonId: number | null = null;
    public orgPersonId: number | null = null;
    public cnTitle: string = '投资机构';
    
    // refs
    public $refs: {
        companyTable: HTMLFormElement;
    };

    // 声明周期钩子
    public mounted() {
        this.eventId = this.$route.params.eventId;
        if (this.eventInvestDto.businessId) {
            this.businessId = this.eventInvestDto.businessId.toString();
            this.getFund();
            this.getManageTeam(2);
            this.getManageTeam(3);
        }
        this.$set(this.companySearch, 'name', this.eventInvestDto.investName);
        switch (this.formTitle) {
            case 'org': 
                this.attribute = 2;
                this.eventInvestDto.businessType = 2;
                this.cnTitle = '投资机构';
                break;
            case 'investor': 
                this.attribute = 15;
                this.eventInvestDto.businessType = 15;
                this.cnTitle = '投资人';
                break;
            case 'enterprise':
                this.attribute = 1;
                this.eventInvestDto.businessType = 1;
                this.cnTitle = '企业名称';
                break;
            default:
                this.attribute = 15;
                this.eventInvestDto.businessType = 1;
                this.cnTitle = '投资机构';
                break;
        }
    }
    // 方法
    public selectTab(tab: {index: string}) {
        this.tabIndex = tab.index;
    }
    // 保存
    public saveInvestor() {
        if (!this.businessId) {
            this.$message.warning('请选择' + this.cnTitle);
        } else if (!this.eventInvestDto.investMoney && !this.eventInvestDto.investEstimate) {
            this.$message.warning('请填写实际投资金额或估计投资金额');
        } else if (!this.eventInvestDto.currencyType) {
            this.$message.warning('请选择币种');
        } else {
            this.eventInvestDto.businessId = Number(this.businessId);
            this.eventInvestDto.eventId = Number(this.eventId);
            this.eventInvestDto.fundId = this.fundId ? Number(this.fundId) : 0;
            this.eventInvestDto.investName = this.companySearch.name;
            if (this.eventInvestDto.id) {
                this.updateInvestor();
            } else {
                this.addInvestor();
            }
        }
        
    }
    // 添加投资方
    public async addInvestor() {
        const {resCode} = await EventInvestorService.addInvestorEvent(this.eventInvestDto);
        if (resCode === '0') {
            this.$message.success('添加成功！');
            this.closeDialog(true);
        }
    }
    // 修改投资方
    public async updateInvestor() {
        const {resCode} = await EventInvestorService.updateEventInvest(this.eventInvestDto);
        if (resCode === '0') {
            this.$message.success('修改成功！');
            this.closeDialog(true);
        }
    }
    // 查询企业信息--获取企业名字
    public async getEnterprise() {
        this.enterpriseDialog = true;
        this.mainCompanySearch = {
            name: this.companySearch.name ? this.companySearch.name : '',
            searchType: this.companySearch.searchType,
            attribute: this.attribute,
        };
        this.$nextTick( () => {
            this.$refs.companyTable.getCompanyOpenSearchData(this.mainCompanySearch);
        });
    }
    // 获取企业id+name
    public getName(company: {id: number, name: string}) {
        this.companySearch.name = company.name;
        this.businessId = company.id.toString();
        this.enterpriseDialog = false;
        if (this.formTitle === 'org') { // 只有机构才获取基金 机构经理人 基金经理人
            this.getFund();
            this.getManageTeam(2);
            this.getManageTeam(3);
        }
    }
    // 获取人物id+name
    public getPersonName(personId: number, personName: string) {
        this.companySearch.name = personName;
        this.businessId = personId.toString();
        this.personInfoDialog = false;
    }
    // 获取基金
    public async getFund() {
        const res = await ComFundService.getFundInfo(Number(this.businessId));
        if (res.resCode) {
            this.fundId = res.data.id; // 基金ID
            this.companyFundName = res.data.fundName; // 基金Name
        }
    }
    // 获取机构经理人，基金经理人
    public async getManageTeam(type: number) {
        this.orgPersonIds = [];
        this.fundPersonIds = [];
        this.manageSearch['pageParam.pageIndex'] = 1;
        this.manageSearch['pageParam.pageParam'] = 10;
        this.manageSearch.companyId = Number(this.businessId);
        this.manageSearch.type = type;
        const res = await CommonService.getManageTeam(this.manageSearch);
        if (res.resCode === '0') {
            if ( type === 2 ) { // 机构
                this.orgPersonIds = res.data.resultData;
            } else if (type === 3) { // 基金
                this.fundPersonIds = res.data.resultData;
            }
        }
    }
    // 关闭添加投资方弹窗
    public closeDialog(isGetPage: boolean) {
        this.companySearch.name = '';
        this.$emit('closeDialog', isGetPage);
    }
    // 获取投资人信息
    public async getPersonInfo() {
        this.personInfoDialog  = true;
        this.personSearchDto['pageParam.pageIndex'] = 1;
        this.personSearchDto['pageParam.pageParam'] = 10;
        this.personSearchDto.selectType = 'cn_name';
        this.personSearchDto.selectValue = this.companySearch.name ? this.companySearch.name : '';
        this.personSearchDto.companyId = this.businessId ? this.businessId : '-1';
        this.personSearchDto.attr = -1;
        const res = await EventService.getInvestorInvestor(this.personSearchDto);
        if (res.resCode === '0') {
            this.personInfoData = res.data.resultData;
        }
    }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>
